﻿using Edge.Repository.Model.CameraSys;
using LibVLCSharp.Shared;
using System;
using System.Windows.Forms;

namespace CameraClient.CForm.CameraF
{
    public partial class FormCameraFull : Form
    {

        public CameraConfigModel CameraConfig;

        LibVLC _libVlc;
        MediaPlayer _mediaPlayer;
        bool isMainControl = false;


        public FormCameraFull(CameraConfigModel cameraConfigModel)
        {
            InitializeComponent();

            CameraConfig = cameraConfigModel;

            this.Text = cameraConfigModel.Name;
            #region LibVLC

            _libVlc = new LibVLC();
            _mediaPlayer = new MediaPlayer(_libVlc)
            {
                Volume = 0, //静音
                EnableHardwareDecoding = isMainControl,//硬件加速
                                                       // VideoFilter = "format=gray"

            };
            videoViewMain.MediaPlayer = _mediaPlayer;

            Load += OnLoad;
            HandleDestroyed += OnHandleDestroyed;

            #endregion


        }

        private void FormCameraFull_Load(object sender, EventArgs e)
        {
            play();
        }

        #region MyRegion

        private void OnLoad(object sender, EventArgs e)
        {
            DoubleBuffered = true;
            videoViewMain.Enabled = false;
        }

        private void OnHandleDestroyed(object sender, EventArgs args)
        {
            _mediaPlayer.Stop();
            _mediaPlayer.Dispose();
            _libVlc.Dispose();
        }
        public void play()
        {
            var options = new[]
    {
    "no-audio",
    "file-caching=300",
    "live-capture-caching=300",
    "disc-caching-caching=300",
    "network-caching=333",
    "live-caching=300",
     isMainControl   ? "avcodec-hw=any" : "" //硬解码
};
            // using (var media = new Media(_libVlc, new Uri(myUrl), options))
            using (var media = new Media(_libVlc, new Uri(CameraConfig.Source), options))
            {
                _mediaPlayer.Play(media);
            }

        }


        public void stop()
        {
            if (_mediaPlayer.IsPlaying)
            {
                _mediaPlayer.Stop();
            }

        }



        #endregion

        private void FormCameraFull_FormClosing(object sender, FormClosingEventArgs e)
        {
            stop();
        }
    }
}
